/*
 * MIT License
 *
 * Copyright (c) 2023-present, tangli
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

@file:JvmName("Enums")

package tony.core.enums

/**
 * 全局枚举
 * @author tangli
 * @date 2023/09/13 19:16
 */
import com.fasterxml.jackson.annotation.JsonValue
import java.io.Serializable
import java.lang.reflect.Field
import java.util.concurrent.ConcurrentHashMap
import kotlin.collections.getOrPut
import kotlin.jvm.java

/**
 * 全局枚举接口.
 * 已和 jackson 和 mybatis-plus 结合.
 * @param [T] 枚举值类型.
 * @author tangli
 * @date 2023/09/13 19:16
 */
public sealed interface EnumValue<T : Serializable> {
    @get:JsonValue
    public val value: T?
}

/**
 * 全局整形枚举接口.
 * @author tangli
 * @date 2023/09/13 19:16
 */
public interface IntEnumValue : EnumValue<Int>

/**
 * 全局字符串枚举接口.
 * @author tangli
 * @date 2023/09/13 19:16
 */
public interface StringEnumValue : EnumValue<String>

/**
 * 通过枚举值获取在类[EnumValue]中的字段
 * @return [Field]?
 * @author tangli
 * @date 2023/09/13 19:26
 */
public fun <T : EnumValue<*>> T.field(): Field? =
    this::class.java.getField((this as Enum<*>).name)

/**
 * [EnumValue] 可以被视为 null 值
 * @return [Boolean]
 * @author tangli
 * @date 2023/09/13 19:26
 */
public fun <T> T.isDefaultAsNull(): Boolean where T : EnumValue<*>? =
    this == null ||
        this.field()?.let { it.getDeclaredAnnotation(DefaultAndAsNull::class.java) != null } == true

@get:JvmSynthetic
internal val enumMap = ConcurrentHashMap<Class<out EnumValue<*>>, Map<String, *>>()

/**
 * 枚举映射
 * @param [T] 枚举 [Class]
 * @param [E] [EnumValue] 类型
 * @return [Map]<[String], [E]> 类型为键, 实际枚举为值的 map
 * @author tangli
 * @date 2025/09/09 21:40
 */
@Suppress("UNCHECKED_CAST")
public fun <T, E> T.enumsMap(): Map<String, E>
    where E : EnumValue<*>,
          T : Class<out E> =
    enumMap.getOrPut(this) {
        enumConstants.associateBy { it.value.toString() }
    } as Map<String, E>

/**
 * 枚举
 * @param [T] 枚举 [Class]
 * @param [E] [EnumValue] 类型
 * @param [value] 枚举值
 * @return [E]? 枚举
 * @author tangli
 * @date 2025/09/09 21:42
 */
public fun <T, E> T.getEnum(value: Any?): E?
    where E : EnumValue<*>,
          T : Class<out E> =
    enumsMap()[value.toString()]

/**
 * 枚举值列表
 * @param [T] 枚举 [Class]
 * @param [E] [EnumValue][V] 类型
 * @param [V] 枚举值类型
 * @return [List]<[V]> 枚举值[V]列表
 * @author tangli
 * @date 2025/09/09 20:53
 */
@JvmOverloads
@Suppress("UNCHECKED_CAST")
public fun <T, E, V> T.enumValues(
    includeDefault: Boolean = false,
): List<V>
    where V : Serializable,
          E : EnumValue<V>,
          T : Class<out E> =
    enumsMap()
        .values
        .filter { enum ->
            if (includeDefault) {
                true
            } else {
                !enum.isDefaultAsNull()
            }
        }.map { it.value as V }
